package com.zanfeng.signleplatform.modules.auth.controller;

import com.zanfeng.signleplatform.api.io.modules.auth.*;
import com.zanfeng.signleplatform.api.result.modules.account.AccountResult;
import com.zanfeng.signleplatform.api.result.modules.auth.AuthInfoResult;
import com.zanfeng.signleplatform.api.result.modules.auth.AuthLoginResult;
import com.zanfeng.signleplatform.api.result.modules.auth.RoleModulePermissionResult;
import com.zanfeng.signleplatform.api.result.modules.module.ModuleResult;
import com.zanfeng.signleplatform.api.result.modules.permission.PermissionResult;
import com.zanfeng.signleplatform.api.result.modules.role.RoleResult;
import com.zanfeng.signleplatform.api.service.modules.auth.AuthService;
import com.zanfeng.signleplatform.base.BaseController;
import com.zanfeng.signleplatform.common.auth.ApiTokenResult;
import com.zanfeng.signleplatform.common.utils.NetworkUtil;
import com.zanfeng.signleplatform.auth.AuthToken;
import com.zanfeng.signleplatform.auth.WtAuthorization;
import com.zanfeng.signleplatform.io.PageListIO;
import com.zanfeng.signleplatform.io.SimpleIO;
import com.zanfeng.signleplatform.io.SimpleListIO;
import com.zanfeng.signleplatform.results.ApiResult;
import com.zanfeng.signleplatform.results.FormListResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(value = "AuthController", description = "[后台]后台管理")
@RestController
@RequestMapping("/modules/auth")
public class AuthController extends BaseController {

    @Resource
    private AuthService authService;

    @ApiOperation(value = "用户登录", notes = "使用用户密码进行登录", response = AuthLoginResult.class)
    @PostMapping("/login")
    public ApiResult login(@ApiParam(required = true) @Valid @RequestBody AuthLoginIO body) {
        AuthLoginResult result = authService.login(body.getUsername(), body.getPassword(), NetworkUtil.getIpAddress(request));
        return ApiResult.success(result);
    }

    @ApiOperation(value = "用户授权信息", notes = "用户授权信息", response = AuthInfoResult.class)
    @WtAuthorization
    @PostMapping("/authInfo")
    public ApiResult authInfo(@AuthToken ApiTokenResult authToken) {
        AuthInfoResult result = authService.getAuthInfo(Integer.valueOf(authToken.getUserId()));
        return ApiResult.success(result);
    }

    @ApiOperation(value = "更新用户基本信息", notes = "更新用户基本信息", response = ApiResult.class)
    @WtAuthorization
    @PostMapping("/updateAccountBasic")
    public ApiResult updateAccountBasic(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody AccountDetailIO body) {
        AccountDetailIO io = new AccountDetailIO();
        io.setId(Integer.valueOf(authToken.getUserId()));
        io.setNewPassword(body.getNewPassword());
        io.setConfirmPassword(body.getConfirmPassword());
        io.setRealname(body.getRealname());
        io.setRemark(body.getRemark());
        authService.updateAccount(io);
        return ApiResult.success();
    }

    /* -----------------------------  用户相关 开始 -----------------------------  */
    @ApiOperation(value = "用户列表", notes = "用户列表", response = AccountResult.class)
    @PostMapping("accountList")
    @WtAuthorization
    public ApiResult accountList(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PageListIO<AuthFormIO> body) {
        FormListResult<AccountResult> result = authService.queryAccountPageList(body);
        return ApiResult.success(result);
    }

    @ApiOperation(value = "用户详情", notes = "用户详情", response = AccountResult.class)
    @PostMapping("/accountDetail")
    @WtAuthorization
    public ApiResult accountDetail(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleIO body) {
        AccountResult accountResult = authService.findAccountResultById(NumberUtils.toInt(body.getId()));
        return ApiResult.success(accountResult);
    }

    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = ApiResult.class)
    @PostMapping("/accountUpdate")
    @WtAuthorization
    public ApiResult accountUpdate(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody AccountDetailIO body) {
        authService.updateAccount(body);
        return ApiResult.success();
    }

    @ApiOperation(value = "新增用户", notes = "新增用户", response = ApiResult.class)
    @PostMapping("/accountAdd")
    @WtAuthorization
    public ApiResult accountAdd(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody AccountDetailIO body) {
        authService.saveAccount(body);
        return ApiResult.success();
    }

    @ApiOperation(value = "删除用户", notes = "根据用户ID删除用户信息", response = ApiResult.class)
    @PostMapping("/accountDelete")
    @WtAuthorization
    public ApiResult accountDelete(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleListIO body) {
        if (CollectionUtils.isNotEmpty(body.getList())) {
            authService.deleteAccountListByIdList(body.getList());
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "获取用户可用角色列表", notes = "获取用户可用角色列表", response = ApiResult.class)
    @PostMapping("/accountRoleList")
    @WtAuthorization
    public ApiResult accountRoleList(@AuthToken ApiTokenResult authToken) {
        List<RoleResult> roleResultList = authService.queryRoleActiveList();
        return ApiResult.success(roleResultList);
    }
    /* -----------------------------  用户相关 结束 -----------------------------  */


    /* -----------------------------  角色相关 开始 -----------------------------  */
    @ApiOperation(value = "角色列表", notes = "角色列表", response = RoleResult.class)
    @PostMapping("roleList")
    @WtAuthorization
    public ApiResult roleList(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PageListIO<AuthFormIO> body) {
        FormListResult<RoleResult> result = authService.queryRolePageList(body);
        return ApiResult.success(result);
    }

    @ApiOperation(value = "角色详情", notes = "角色详情", response = RoleResult.class)
    @PostMapping("/roleDetail")
    @WtAuthorization
    public ApiResult roleDetail(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleIO body) {
        RoleResult roleResult = authService.findRoleResultById(NumberUtils.toInt(body.getId()));
        return ApiResult.success(roleResult);
    }

    @ApiOperation(value = "角色更新", notes = "角色更新", response = ApiResult.class)
    @PostMapping("/roleUpdate")
    @WtAuthorization
    public ApiResult roleUpdate(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody RoleDetailIO body) {
        authService.updateRole(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "新增角色", notes = "新增角色", response = ApiResult.class)
    @PostMapping("/roleAdd")
    @WtAuthorization
    public ApiResult roleAdd(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody RoleDetailIO body) {
        authService.saveRole(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "删除角色", notes = "根据角色ID删除角色信息", response = ApiResult.class)
    @PostMapping("/roleDelete")
    @WtAuthorization
    public ApiResult roleDelete(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleListIO body) {
        if (CollectionUtils.isNotEmpty(body.getList())) {
            authService.deleteRoleListByIdList(body.getList());
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "获取角色全部可用菜单列表", notes = "获取角色全部可用菜单列表", response = RoleModulePermissionResult.class)
    @ApiImplicitParams({})
    @PostMapping("/roleModuleList")
    @WtAuthorization
    public ApiResult roleModuleList(@AuthToken ApiTokenResult authToken) {
        RoleModulePermissionResult result = authService.findRoleModulePermissionResult(authToken);
        return ApiResult.success(result);
    }
    /* -----------------------------  角色相关 结束 -----------------------------  */

    /* -----------------------------  菜单相关 开始 -----------------------------  */
    @ApiOperation(value = "菜单列表", notes = "菜单列表", response = ModuleResult.class)
    @PostMapping("moduleList")
    @WtAuthorization
    public ApiResult moduleList(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PageListIO<AuthFormIO> body) {
        FormListResult<ModuleResult> result = authService.queryModulePageList(body);
        return ApiResult.success(result);
    }

    @ApiOperation(value = "菜单详情", notes = "菜单详情", response = ModuleResult.class)
    @PostMapping("/moduleDetail")
    @WtAuthorization
    public ApiResult moduleDetail(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleIO body) {
        ModuleResult moduleResult = authService.findModuleResultById(NumberUtils.toInt(body.getId()));
        return ApiResult.success(moduleResult);
    }

    @ApiOperation(value = "菜单更新", notes = "菜单更新", response = ApiResult.class)
    @PostMapping("/moduleUpdate")
    @WtAuthorization
    public ApiResult moduleUpdate(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody ModuleDetailIO body) {
        authService.updateModule(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "新增菜单", notes = "新增菜单", response = ApiResult.class)
    @PostMapping("/moduleAdd")
    @WtAuthorization
    public ApiResult moduleAdd(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody ModuleDetailIO body) {
        authService.saveModule(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "删除菜单", notes = "根据菜单ID删除菜单信息", response = ApiResult.class)
    @PostMapping("/moduleDelete")
    @WtAuthorization
    public ApiResult moduleDelete(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleListIO body) {
        if (CollectionUtils.isNotEmpty(body.getList())) {
            int count = authService.countChildrenModuleListByIdList(body.getList());
            if (count > 0) {
                return ApiResult.fail("请先移除子节点");
            }
            authService.deleteModuleListByIdList(body.getList());
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "菜单节点列表", notes = "菜单节点列表", response = ModuleResult.class)
    @PostMapping("moduleNodeList")
    @WtAuthorization
    public ApiResult moduleNodeList(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleIO body) {
        List<ModuleResult> resultList = authService.queryModuleNodeList(body.getId());
        return ApiResult.success(resultList);
    }

    /* -----------------------------  菜单相关 结束 -----------------------------  */


    /* -----------------------------  权限相关 开始 -----------------------------  */
    @ApiOperation(value = "角色列表", notes = "角色列表", response = PermissionResult.class)
    @PostMapping("permissionList")
    @WtAuthorization
    public ApiResult permissionList(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PageListIO<AuthFormIO> body) {
        FormListResult<PermissionResult> result = authService.queryPermissionPageList(body);
        return ApiResult.success(result);
    }

    @ApiOperation(value = "角色详情", notes = "角色详情", response = PermissionResult.class)
    @PostMapping("/permissionDetail")
    @WtAuthorization
    public ApiResult permissionDetail(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleIO body) {
        PermissionResult permissionResult = authService.findPermissionResultById(NumberUtils.toInt(body.getId()));
        return ApiResult.success(permissionResult);
    }

    @ApiOperation(value = "角色更新", notes = "角色更新", response = ApiResult.class)
    @PostMapping("/permissionUpdate")
    @WtAuthorization
    public ApiResult permissionUpdate(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PermissionDetailIO body) {
        authService.updatePermission(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "新增角色", notes = "新增角色", response = ApiResult.class)
    @PostMapping("/permissionAdd")
    @WtAuthorization
    public ApiResult permissionAdd(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody PermissionDetailIO body) {
        authService.savePermission(authToken, body);
        return ApiResult.success();
    }

    @ApiOperation(value = "删除角色", notes = "根据角色ID删除角色信息", response = ApiResult.class)
    @PostMapping("/permissionDelete")
    @WtAuthorization
    public ApiResult permissionDelete(@AuthToken ApiTokenResult authToken, @ApiParam(required = true) @RequestBody SimpleListIO body) {
        if (CollectionUtils.isNotEmpty(body.getList())) {
            authService.deletePermissionListByIdList(body.getList());
        }
        return ApiResult.success();
    }

    /* -----------------------------  权限相关 结束 -----------------------------  */

}
